package db

import (
	"com.wen/model"
	"github.com/jmoiron/sqlx"
)

func InsertCategory(category *model.Category) (categoryId int64, err error) {

	sqlstr := `insert into category(category_name, category_no)values(?,?)`
	result, err := DB.Exec(sqlstr, category.CategoryName, category.CategoryNo)
	if err != nil {
		return
	}

	categoryId, err = result.LastInsertId()
	return
}

func GetCategoryList(categoryIds []int64) (categoryList []*model.Category, err error) {

	sqlstr, args, err := sqlx.In(`select id, category_name, category_no from category where id in(?)`, categoryIds)
	if err != nil {
		return
	}

	err = DB.Select(&categoryList, sqlstr, args...)
	return
}

func GetAllCategoryList() (categoryList []*model.Category, err error) {
	sqlstr := "select id, category_name, category_no from category order by category_no asc"
	err = DB.Select(&categoryList, sqlstr)
	return
}
